﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Interop;
using System;
using Microsoft.Samples;
using System.Windows.Media.Animation;

namespace PixelShaderWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Create Screen Shot
            Bitmap bmp = CaptureScreen();
            BitmapSource bmpSrc = CreateBitmapSource(bmp);
            backgroundImage.Source = bmpSrc;

            this.Loaded += (s, e) =>
                {
                    // Activate Fullscreen
                    FullScreenHelper.GoFullscreen(this);
                };
        }

        public Bitmap CaptureScreen()
        {
            Bitmap bmp = new Bitmap(SystemInformation.VirtualScreen.Width, 
                SystemInformation.VirtualScreen.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
            g.Dispose();
            return bmp;
        }

        public static BitmapSource CreateBitmapSource(System.Drawing.Bitmap bmp)
        {
            return Imaging.CreateBitmapSourceFromHBitmap(
                bmp.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }

        private void closeWindow_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void blurEffect_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Effects.BlurEffect blur = 
                new System.Windows.Media.Effects.BlurEffect();
            blur.Radius = 20;
            backgroundImage.Effect = blur;
        }
        
        private void monochromeEffect_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Samples.ShaderEffects.MonochromeEffect monochrome = 
                new Microsoft.Samples.ShaderEffects.MonochromeEffect();
            backgroundImage.Effect = monochrome;            
        }

        private void resetEffects_Click(object sender, RoutedEventArgs e)
        {
            this.MouseMove -= MainWindow_MouseMove;
            this.MouseLeftButtonDown -= MainWindow_MouseLeftButtonDown;
            backgroundImage.Effect = null;
        }

        private void pixelateEffect_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Samples.ShaderEffects.PixelateEffect pixelate = new Microsoft.Samples.ShaderEffects.PixelateEffect();
            backgroundImage.Effect = pixelate;
        }

        private void rippleEffect_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Samples.ShaderEffects.RippleEffect ripple = new Microsoft.Samples.ShaderEffects.RippleEffect();
            backgroundImage.Effect = ripple;
        }

        private void rippleEffectMouseMove_Click(object sender, RoutedEventArgs e)
        {
            this.MouseMove += new System.Windows.Input.MouseEventHandler(MainWindow_MouseMove);
        }

        void MainWindow_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            System.Windows.Point p = e.GetPosition(this);
            Microsoft.Samples.ShaderEffects.RippleEffect ripple = new Microsoft.Samples.ShaderEffects.RippleEffect();

            // Center of image in percent
            double centerX = p.X / backgroundImage.ActualWidth;
            double centerY = p.Y / backgroundImage.ActualHeight;

            ripple.Center = new System.Windows.Point(centerX, centerY);
            ripple.Amplitude = .5;
            ripple.Frequency = 50;
            ripple.Phase = 0;
            
            backgroundImage.Effect = ripple;
        }

        private void rippleEffectMouseClick_Click(object sender, RoutedEventArgs e)
        {
            this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
        }

        void MainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            System.Windows.Point p = e.GetPosition(this);
            Microsoft.Samples.ShaderEffects.RippleEffect ripple = new Microsoft.Samples.ShaderEffects.RippleEffect();
            
            double centerX = p.X / backgroundImage.ActualWidth;
            double centerY = p.Y / backgroundImage.ActualHeight;
            ripple.Center = new System.Windows.Point(centerX, centerY);
            ripple.Amplitude = .5;
            ripple.Frequency = 0;
            ripple.Phase = 0;
            
            backgroundImage.Effect = ripple;

            DoubleAnimation da = new DoubleAnimation();
            da.From = 0;
            da.To = 25;
            da.AutoReverse = true;

            da.Duration = new Duration(TimeSpan.FromSeconds(3));
            ripple.BeginAnimation(Microsoft.Samples.ShaderEffects.RippleEffect.FrequencyProperty,
                da);
        }
    }
}
